package com.core.family.admin.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.core.common.gen.BaseServiceImpl;
import com.core.family.admin.domain.dto.GroupDto;
import com.core.family.admin.domain.dto.UserGp;
import com.core.family.admin.domain.po.SysGroup;
import com.core.family.admin.domain.po.SysUserGroup;
import com.core.family.admin.mapper.SysUserGroupMapper;
import com.core.family.admin.service.SysGroupService;
import com.core.family.admin.service.SysUserGroupService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.util.List;


/**
 * @author honglei
 * @version 1.0
 * @time: 2022-6-10 11:46:34
 * @Description 服务实现层
 */
@Service
public class SysUserGroupServiceImpl extends BaseServiceImpl<SysUserGroupMapper, SysUserGroup> implements SysUserGroupService {

    @Autowired
    SysGroupService groupService;

    @Override
    public List<SysUserGroup> getByUserId(Integer orgId, Serializable id) {

        return list(new LambdaQueryWrapper<SysUserGroup>()
                .eq(SysUserGroup::getOrgId, orgId)
                .eq(SysUserGroup::getUid, id));
    }

    @Override
    public int saveUserGp(UserGp userGp) {
        SysGroup group = new SysGroup();
        group.setOrgId(userGp.getOrgId());
        group.setGCode(userGp.getGCode());
        group.setGName(userGp.getGname());
        groupService.saveOrUpdate(group);

        userGp.setGid(group.getId());


        if (!CollectionUtils.isEmpty(list(new LambdaQueryWrapper<SysUserGroup>()
                .eq(SysUserGroup::getGid, userGp.getGid())
                .eq(SysUserGroup::getUid, userGp.getUid())
                .eq(SysUserGroup::getOrgId, userGp.getOrgId())))) {
            return group.getId();
        }

        SysUserGroup userGroup = new SysUserGroup();
        BeanUtils.copyProperties(userGp, userGroup);

        save(userGroup);

        return group.getId();
    }
}

